package base.core.api;


import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.servlet.http.HttpServletResponse;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * @Description
 * @Author Pengwei
 * @CreatDate 2021/12/25 02:40
 */
@Data
@Accessors(chain = true)
public class Result<T> {

    @Schema(description = "(http)状态码: 200-成功; 500-默认失败; " )
    Integer code = HttpServletResponse.SC_OK;

    @Schema(description = "是否成功")
    boolean success = true;

    @Schema(description = "错误提示,失败时有效")
    String msg;

    @Schema(description = "数据结构体,成功时有效")
    T data;

    @Hidden
    String exception;

    public static <T> Result<T> success(T data) {
        return new Result<T>().setData(data);
    }

    public static <T> Result<T> success() {
        return new Result<T>();
    }

    public static Result failed() {
        return new Result().setCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).setSuccess(false);
    }

    public static <T> Result<T> ofBol(Boolean success) {
        if (success == null || !success) {
            return failed();
        }
        return success();
    }
}